# _*_ coding: utf-8 _*_
from odoo import models, fields, api


class StockPickingType(models.Model):
    _inherit = "stock.picking.type"

    count_picking_todo = fields.Integer(compute="_compute_count_picking_todo")
    enable_todo = fields.Boolean(string=u"启用单据提交")

    @api.multi
    def _compute_count_picking_todo(self):
        domains = {
            'count_picking_todo': [('is_todo', '=', 'True')],
        }
        for field in domains:
            data = self.env['stock.picking'].read_group(domains[field] +
                                                        [('state', 'not in', ('done', 'cancel')),
                                                         ('picking_type_id', 'in', self.ids)],
                                                        ['picking_type_id'], ['picking_type_id'])
            count = dict(
                map(lambda x: (x['picking_type_id'] and x['picking_type_id'][0], x['picking_type_id_count']), data))
            for record in self:
                record[field] = count.get(record.id, 0)

    @api.multi
    def get_action_picking_tree_todo(self):
        return self._get_action('stock_picking_todo.action_picking_tree_todo')
